#include "pushfile.h"

//********************************************************************************
//推送文件函数
//********************************************************************************
void PushFile(int peerfd,char* filename)
{
    //1.发送文件长度
    int fd = open(filename,O_RDWR);
    if(fd == -1)
    {
        //先不考虑发送信息给对端
        printf("error filename\n");
        return;
    }
    struct stat st;
    fstat(fd,&st);
    send(peerfd,&st.st_size,8,MSG_WAITALL);

    //2.发送文件内容
    size_t filelength = st.st_size;
    train_t train;
    int length = 1024;
    while(filelength > 0)
    {
        //发送小火车
        memset(&train,0,sizeof(train));
        if(filelength > length)
        {
            train.len = 1024;
        }
        else
        {
            train.len = filelength;
        }
        int ret = read(fd,train.data,train.len);
        send(peerfd,&train,train.len + 4,MSG_WAITALL);
        filelength -= train.len;
    }
    close(fd);
}

